<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import Login from './views/Login.vue'
import MainLayout from './components/MainLayout.vue'
import CourseManage from './views/CourseManage.vue'

const authed = ref(!!localStorage.getItem('token'))
const router = useRouter()

function onLoginSuccess() {
  authed.value = true
  router.replace('/home')
}

onMounted(() => {
  // 监听 401 未授权事件，返回登录界面
  window.addEventListener('auth:unauthorized', () => {
    authed.value = false
  })
})
</script>

<template>
  <Login v-if="!authed" @success="onLoginSuccess" />
  <MainLayout v-else />
</template>

<style scoped>
</style>
